import { reactive } from "vue"
import { useStore } from "vuex"
import NetFunc from "./NetFunc"
import { useRouter } from "vue-router"
export default {
    template: `
    <div class="main">
    <div class="login">
    <div class="phone"><input type="text" placeholder="手机号" v-model="user.phone"></div>
    <div class="password"><input type="password" placeholder="密码" v-model="user.psw"></div>
    <div class="username"><input type="text" placeholder="用户名" v-model="user.name"></div>
    <div class="ava"><input type="text" placeholder="头像链接地址" v-model="user.ava"></div>
    <button @click="TapLogin">去登录</button>
    <button @click="TapReg">注册</button>
    </div>
    <br>
    {{user.err}}
    </div>
    `,
    setup() {
        const user = reactive({ phone: '', psw: '',name:'',ava:'', err: '' })
        const router = useRouter()
        const store = useStore()
        const NF = new NetFunc()
        async function PostUser() {
            const url='http://localhost:3300/r/addUser'
            const opt = {
                username:user.name,
                phone: user.phone,
                password: user.psw,
                ava:user.ava
            }
            const g = await NF.PostUrl(url,opt)
            const msg=g.msg
            // if(g.success==0){
            //     alert('该手机号已被注册')
            // }else{
            //     router.push({name:'index'})
            // }
            console.log(msg)
            return
        }
        function TapLogin() {
            router.push({name:'login'})
        }
        function TapReg() {
            if (user.phone.length !=11 || user.psw.length < 5||user.name.length<1) {
                alert('填写信息有误')
                return
            } else if(user.ava.length<10){
                user.ava='https://img1.baidu.com/it/u=2250275304,3941496093&fm=253&fmt=auto&app=138&f=GIF?w=200&h=200'
                PostUser()
                router.push({name:'index'})
            } else{
                PostUser()
                router.push({name:'index'})
            }
        }
        return { user, TapLogin,TapReg,PostUser}

    }
}
